/* Edge.java */

package graph;

/** 
 * An edge represents a connection between two nodes in a graph.
 * An edge has references to each of its nodes, a reference
 * to its equivalent partner, and a weight value.
 */

class Edge {
  private int weight;
  private VertexPair pair;
  
  /** 
   * Edge() constructs an edge given a VertexPair and a weight;
   */
  public Edge(VertexPair pair, int weight) {
    this.pair = pair;
    this.weight = weight;
  }
  
  /**
   * equals() returns true if this Edge represents the same unordered
   * pair of objects as the parameter "o".  The order of the pair does not
   * affect the equality test, so (u, v) is found to be equal to (v, u).
   */
  public boolean equals(Object o) {
    if (o instanceof Edge) {
      return pair.equals(((Edge) o).pair);
    } else {
      return false;
    }
  }
  
  /**
   * Returns the weight of the edge.
   */
  public int weight() {
    return weight;
  }
  
  /** 
   * Changes the weight variable to i.
   */
  public void setWeight(int i) {
	  weight = i;
  }

  /**
   * Returns a string representation of this object.
   */
  public String toString() {
    return "[" + pair + ", " + weight + "]";
  }
  
  /**
   * Returns the vertex pair associated with the this edge.
   */
  public VertexPair getVertex() {
	  return pair;
  }
  
  /**
   * Given one of the objects in the edge, this method
   * returns the other object. If the object is not in the
   * edge the method silently returns null.
   */
  public Object getNeighbor(Object o) {
    if (o.equals(pair.object1)) {
      return pair.object2;
    } else if (o.equals(pair.object2)) {
      return pair.object1;
    } else {
      return null;
    }
  }
  
  
  
  public static void main(String[] args) {
    Integer a = 1;
    Integer b = 2;
    Edge edge1 = new Edge(new VertexPair(a, b), 100);
    Edge edge2 = new Edge(new VertexPair(b, a), 100);
    System.out.println("Checking Weight");
    System.out.println("Should be 100: " + edge1.weight());
    System.out.println("Checking Equality");
    System.out.println("Should be true: " + (edge1.weight() == edge2.weight()));
    System.out.println("Should be true: " + (edge1.equals(edge2)));
    System.out.println("Should be 2: " + (Integer) edge1.getNeighbor(a));
    System.out.println("Should be 1: " + (Integer) edge1.getNeighbor(b));
    System.out.println("Should be 2: " + (Integer) edge2.getNeighbor(a));
    System.out.println("Should be 1: " + (Integer) edge2.getNeighbor(b));
    System.out.println("Checking Print Methods");   
    System.out.println("Should be [[1, 2], 100]: " + edge1);    
    System.out.println("Should be [[2, 1], 100]: " + edge2);
  }
}

